<template>
    <div class="lagebox">
        <div class="loginBox">
            <h2>后台管理系统</h2>
            <el-form ref="form" :model="form" :rules="rules">
                <el-form-item prop="username">
                    <el-input prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="el-icon-s-goods" v-model="form.password" type="password" @keyup.enter.native="login"></el-input>
                </el-form-item>
                <el-form-item class="btns">
                    <el-button @click="login">登录</el-button>
                    <el-button @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import Vue from 'vue'

    export default {
        name: "login",
        data() {
            return {
                form: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 5, message: '长度在3-5个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 5, max: 10, message: '长度在5-10个字符', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            login() {
                this.$refs.form.validate(valid => {
                    if (!valid) return;
                    console.log(JSON.stringify(this.form));
                    const that = this;
                    this.$http.post('login', this.form).then((result) => {
                        console.log(result)
                        if (result.data.meta.status == 200) {
                            Vue.prototype.$message({
                                message: '登陆成功',
                                type: 'success'
                            });
                            window.sessionStorage.setItem('token', result.data.data.token);
                            this.$router.replace("/index")
                        } else {
                            Vue.prototype.$message({
                                message: '登录失败',
                                type: 'error'
                            })
                        }
                    })
                })
            },
            reset() {
                this.$refs.form.resetFields()
            }
        }
    }
</script>

<style scoped>
    .lagebox{
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: url("../assets/img/login_bg.jpg") no-repeat;
        background-size: cover;
    }
    .el-form {
        margin-top: 30px;
    }

    .loginBox {
        padding: 20px 20px;
        background-color: #ffffff;
        width: 350px;
        height: 250px;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        /*移动自身*/
        transform: translate(-50%, -50%);
        text-align: center;
    }

    .btns {
        display: flex;
        justify-content: flex-end;
    }
    .el-input__inner{
        border-color: #dcdfe6;
    }
</style>
